<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($this->adminUrl.'/workspaces/new', $this->lang('ap_title_workspaces_new'));

if(Antz_Intelliform::submitted('new-workspace')){
    $users = post('users');

    $dbParams = array(
        'title' => post('title'),
        'is_live' => 0
    );
    $wsid = $DB->insert($CONFIG->dbprefix.'workspaces', $dbParams);

    if(is_array($users)){
        foreach($users as $userid){
            $dbParams = array(
                'user_id' => $userid,
                'workspace_id' => $wsid
            );
            $DB->insert($CONFIG->dbprefix.'admin_users_to_workspaces', $dbParams);
        }
    }

    // create each domain in the workspace
    $domains = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}domains");
    foreach($domains as $k=>$domain){
        $dbParams = array(
            'parent_id' => 0,
            'slug' => $domain['domain_name'],
            'is_published' => 1,
            'workspace_id' => $wsid,
            'owner_id' => $USER->userid,
            'created_tstamp' => time(),
            'modified_tstamp' => time(),
            'is_default' => 1,
            'domain_id' => $domain['id'],
            'sorting' => $k
        );
        $DB->insert($CONFIG->dbprefix.'pages', $dbParams);
    }

    JFX::addSuccess($this->lang('workspace_created_success'));
    JFX::redirect($CONFIG->adminUrl.'/workspaces');
}








$users = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}admin_users ORDER BY username ASC");


$newWorkspaceFormFields = array(
    array(
        'name' => 'title',
        'type' => 'text',
        'value' => post('title'),
        'label' => $this->lang('workspace_title')
    )
);

foreach($users as $k=>$v){
    $newWorkspaceFormFields[] = array(
        'name' => 'users[]',
        'type' => 'checkbox',
        'value' => $v['id'],
        'label' => $v['username']
    );
}

$newWorkspaceFormFields[] = array(
    'name' => 'submitBtn',
    'type' => 'submit',
    'value' => $this->lang('new_workspace_submit'),
    'label' => '&nbsp;'
);

$newWorkspaceForm = JFX::makeRapidForm($newWorkspaceFormFields, $this->lang('new_workspace_legend'), 'new-workspace');

echo $newWorkspaceForm;


















